<?php

namespace app\api\controller;

use app\common\controller\Api;
use fast\Random;
use think\Db;

class UserCopy extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];


    public function reSet()
    {
        $list = Db::name('user')->page(1, 1000)->select();
        foreach ($list as $item){
            $qrcode = $this->qrcode($item['id']);
            Db::name('user')->where('id', $item['id'])->update(['qrcode' => $qrcode]);
        }

        $this->success('ok');
    }

    public function copy()
    {
        $list = Db::name('user_copy1')->select();

        foreach ($list as $value) {

            $username = (substr($value['username'], 0, 1) == '0') ? '6' . $value['username'] : $value['username'];
            $password = 123456;
            $salt = Random::alnum();
            $password = $this->getEncryptPassword($password, $salt);

            if (!Db::name('user')->where('username', $username)->count()) {
                $data = [
                    'group_id' => 0,
                    'username' => $username,
                    'nickname' => $value['nickname'],
                    'password' => $password,
                    'salt' => $salt,
                    'mobile' => $username,
                    'level' => 1,
                    'score' => $value['score'],
                    'status' => 'normal',
                    'createtime' => time(),
                    'updatetime' => time(),
                ];

                $user_id = Db::name('user')->insertGetId($data);

                $qrcode = $this->qrcode($user_id);
                Db::name('user')->where('id', $user_id)->update(['qrcode' => $qrcode]);
            }


        }

        $this->success('success');
    }


    public function getEncryptPassword($password, $salt = '')
    {
        return md5(md5($password) . $salt);
    }


    /**
     * @ApiInternal
     * 生成二维码
     */
    public function qrcode($user_id)
    {
        $result = \Endroid\QrCode\Builder\Builder::create()
            ->writer(new \Endroid\QrCode\Writer\PngWriter())
            ->writerOptions([])
            ->data($user_id)
            ->encoding(new \Endroid\QrCode\Encoding\Encoding('UTF-8'))
            // ->errorCorrectionLevel(new \Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelHigh())
            ->size(300)
            ->margin(10)
            // ->roundBlockSizeMode(new \Endroid\QrCode\RoundBlockSizeMode\RoundBlockSizeModeMargin())

            //// 设置二维码 LOGO 图片
            // ->logoPath(__DIR__.'/icon.png')

            //// 设置二维码标签字段
            // ->labelText('This is the label')
            // ->labelFont(new \Endroid\QrCode\Label\Font\OpenSans(20))
            // ->labelAlignment(new \Endroid\QrCode\Label\Alignment\LabelAlignmentCenter())
            ->validateResult(false)
            ->build();

        // 输出 Base64 图片
        $base64Image = $result->getDataUri();

        // 解码Base64数据
        $imageData = base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $base64Image));

        // 生成文件名
        $fileName = time() . '.png';
        // 保存文件到指定路径
        $filePath = ROOT_PATH . 'public/uploads/qrcode' . $fileName;
        file_put_contents($filePath, $imageData);
        return '/uploads/qrcode' . $fileName;
    }
}